package com.ssb.plugins;

import com.apache.cache.util.Validator;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;
import com.apache.tools.DateUtils;
import com.apache.tools.StrUtil;
import com.ssb.util.DateCalculationUtil;

import java.util.*;

/**
 * @Classname SelectPremiumStatisticalPluginImpl
 * @Description 查询渠道商保费统计走势
 * @Date 2020/4/18 20:20
 * @Created by fpc
 */
public class SelectPremiumStatisticalPluginImpl implements CustomMethodPlugin {
    @Override
    public Object beforeInvoke(Map<String, Object> map) {
        String userId = String.valueOf(map.get("uctUser.userId"));
        String userEname = String.valueOf(map.get("uctUser.userEname"));
        String groupType = String.valueOf(map.get("dateType"));
        String formatGroup = "%Y-%m";
        if("year".equals(groupType)){
            formatGroup = "%Y";
        }
        String startDate = StrUtil.doNull(String.valueOf(map.get("startDate")),"2020-02");
        String endDate = StrUtil.doNull(String.valueOf(map.get("startDate")), DateUtils.Now.fmt_yyyyMM());
        //获取要查询的月份
        List<String> monthList = DateCalculationUtil.getMonthBetween(startDate, endDate);

        //ssInsurOrder
        //获取所有子级渠道商
        List<DataMap> list = selectAllChildLevel(userId);
        String allUserIds="";
        if(list!=null &&list.size()>0){
            for (DataMap data:list) {
                allUserIds+="'"+data.get("userId")+"',";
            }
            allUserIds = allUserIds.substring(0,allUserIds.length()-1);
        }
        String column = " DATE_FORMAT(sio.insure_date, '"+formatGroup+"') AS yearmonth,COUNT(sio.info_id) AS num, SUM(sio.premium) AS premium ";
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT ");
        sb.append(column);
        sb.append("FROM ss_insur_order sio WHERE ");
        sb.append("sio.org_id in ( ");
        sb.append(allUserIds);
        sb.append(") OR sio.create_user='");
        sb.append(userEname);
        sb.append("'");
        sb.append(" AND DATE_FORMAT(sio.insure_date, '%Y-%m') >='"+startDate+"'");
        sb.append(" AND DATE_FORMAT(sio.insure_date, '%Y-%m') <='"+endDate+"'");
        sb.append(" GROUP BY yearMonth");
        List<DataMap> groupList = (List<DataMap>)IusPluginUtil.getInstance().select("ssb", sb.toString(), 0, 0);
        DataMap tempMap =null;
        for (String month:monthList) {
            boolean flag = true;
            if(!Validator.isEmpty(groupList)&&groupList.size()>0){
                group:
                for (DataMap data : groupList) {
                    if (data.get("yearmonth").equals(month)) {
                        flag = false;
                        break group;
                    }
                }
            }
            if(flag){
                tempMap = new DataMap();
                tempMap.put("yearmonth",month);
                tempMap.put("num",0);
                tempMap.put("premium",0);
                groupList.add(tempMap);
            }
        }
        Collections.sort(groupList, new Comparator<DataMap>() {
            @Override
            public int compare(DataMap o1, DataMap o2) {
                String yearmonth1 =  String.valueOf(o1.get("yearmonth"));
                String yearmonth2 =  String.valueOf(o2.get("yearmonth"));
                return yearmonth1.compareTo(yearmonth2);
            }
        });
        Map returnMap = new HashMap();
        returnMap.put(CustomMethodPlugin.pluginInnerReturn, groupList);
        returnMap.put(CustomMethodPlugin.resultMark, "T");
        return returnMap;
    }

    @Override
    public Object doInvoke(Map<String, Object> map) {
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> map) {
        return null;
    }
    private List<DataMap> selectAllChildLevel(String userId) {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT user_id,user_cname  FROM uct_user WHERE user_type='broker' AND ");
        sb.append("FIND_IN_SET( user_id, queryChannelChildrenInfo ('");
        sb.append(userId);
        sb.append("'));");
        List<DataMap> cLlist = (List<DataMap>) IusPluginUtil.getInstance().select("plateform",sb.toString(), 0, 0);
        return cLlist;
    }
}
